package com.nashuju.web.controller.login;

import java.util.List;

import org.durcframework.core.DefaultGridResult;
import org.durcframework.core.UserContext;
import org.durcframework.core.controller.SearchController;
import org.durcframework.core.expression.ExpressionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.sys.SysFunction;
import com.nashuju.web.entity.sys.SysRes;
import com.nashuju.web.entity.user.User;
import com.nashuju.web.service.sys.SysFunctionService;
import com.nashuju.web.service.sys.SysResService;
import com.nashuju.web.util.TreeUtil;

@Controller
public class MenuController extends SearchController<SysRes, SysResService> {

    @Autowired
    private SysFunctionService sysFunctionService;

    /**
     * 加载用户菜单
     * @return
     */
    @RequestMapping("listUserMenu.do")
    public @ResponseBody Object listUserMenu() {
        User user = UserContext.getInstance().getUser();

        if (user == null) {
            return error("当前用户不存在");
        }

        List<SysRes> menuList = this.getService().getUserMenu(user.getUsername());

        return menuList;
    }

    // 获取所有菜单
    @RequestMapping("/listAllMenu.do")
    public @ResponseBody Object listAllMenu() {

        ExpressionQuery query = ExpressionQuery.buildQueryAll();

        DefaultGridResult resultGrid = (DefaultGridResult) this.queryAll(query);
        @SuppressWarnings("unchecked")
        List<SysRes> rows = (List<SysRes>) resultGrid.getRows();

        List<SysFunction> sysFuns = null;
        for (SysRes rSysRes : rows) {
            sysFuns = sysFunctionService.getBySySResId(rSysRes.getSrId());
            rSysRes.setSysFuns(sysFuns);
        }

        return TreeUtil.buildTreeData(rows);
    }



}
